knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
library(tidyverse)
library(here)
library(broom)
library(janitor)
# Spatial data packages
library(sf)
library(tmap)
For this analysis we will use two datasets:
California Department of Fish and Wildlife. (2020) Oil Spill
Incident Tracking (oil_spill_incident_tracking_[ds394].csv)(accessed here).
California County shapefile (ca_counties_tiger_2016.shp) from the
US Census Bureau’s 2016 MAF/TIGER database (accessed here)
The DFW covers all of the state of California. Each point in this
dataset represents an individual oil spill incident defined as, “a
discharge or threatened discharge of petroleum or other deleterious
material into the waters of the state.”
#### Download, read, and transform data
### read in Oil Spill Incidents csv:
oil_spills_df <- read_csv(here('data/oil_spill_incident_tracking_[ds394].csv')) %>%
clean_names()
#remove 00:00:00+00 from date for tmap labelling purposes
oil_spills_df$dateofinci <- str_remove(oil_spills_df$dateofinci, "00:.+")
### read in CA county shapefile:
ca_counties_sf <- read_sf(here('data/ca_counties/CA_Counties_TIGER2016.shp')) %>%
clean_names() %>%
select(namelsad, countyfp) #reorder so that county name is first column (for tmap)
### convert oil_spills_df to sf:
oil_spills_sf <- st_as_sf(oil_spills_df, ### convert to sf
coords = c('x', 'y'), ### specify spatial information for geometry
crs = st_crs(ca_counties_sf)) ### set coord system same as county_sf
tmap_mode(mode = "view") ### set mode to view/interactive
plot1 <- tm_shape(ca_counties_sf) + ### county polygons
tm_borders("lightgrey", lwd = 0.5) + ###border color and width
tm_shape(oil_spills_sf) + ### oil spill points
tm_dots(id = "dateofinci", ### label with dates instead of point id number
col = "darkcyan", ### color points
size = 0.005) ### size of points
tmap_leaflet(plot1) ### render plot1 tmap